package zap0

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"go_logger/zap0/config"
)

func RunZap1() {
	logger := config.InitLog("D:/tmp/logs/zap0/info.log", "D:/tmp/logs/zap0/error.log", "info")
	logger.Infof("----------------------------%d", 34242)

}

func RunZap() {
	writeSyncer := getLogWriter()
	//encoder := zapcore.NewJSONEncoder(zap0.NewProductionEncoderConfig())
	//encoder := zapcore.NewConsoleEncoder(zap0.NewProductionEncoderConfig())
	encoder := getEncoder()
	core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)

	//logger, _ := zap0.NewProduction()
	logger := zap.New(core, zap.AddCaller())

	sugarLogger := logger.Sugar()
	sugarLogger.Debugf("-----------%s", "fdafafa")
	sugarLogger.Infof("-----------%s", "fdafafa")
}

//日志写入的地方
func getLogWriter() zapcore.WriteSyncer {
	//file, _ := os.Create("./test.log")
	//return zapcore.AddSync(file)

	//滚动日志
	lumberJackLogger := &lumberjack.Logger{
		Filename:   "D:/tmp/logs/test.log",
		MaxSize:    10,
		MaxBackups: 5,
		MaxAge:     30,
		Compress:   false,
	}
	return zapcore.AddSync(lumberJackLogger)

	//	Lumberjack Logger采用以下属性作为输入:
	//
	//Filename: 日志文件的位置
	//	MaxSize：在进行切割之前，日志文件的最大大小（以MB为单位）
	//	MaxBackups：保留旧文件的最大个数
	//	MaxAges：保留旧文件的最大天数
	//	Compress：是否压缩/归档旧文件
}

//日志格式
func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewConsoleEncoder(encoderConfig)
}
